﻿namespace VividKit.PresentationModel.Navigation
{
	using System;
	using Windows.UI.Xaml.Controls;

	/// <summary>
	/// Provides default implementation for the <see cref="INavigationService"/> interface.
	/// </summary>
	public class FrameNavigationService : INavigationService
	{
		private readonly Frame frame;

		/// <summary>
		/// Initializes a new instance of the <see cref="FrameNavigationService"/> class.
		/// </summary>
		/// <param name="frame">The navigation service.</param>
		public FrameNavigationService(Frame frame)
		{
			this.frame = frame;
			SuspensionManager.RegisterFrame(this.frame, "AppFrame");
		}

		/// <summary>
		/// Gets a value that indicates whether there is at least one entry in back navigation history.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if there is at least one entry in back navigation history; otherwise, <c>false</c>.
		/// </value>
		public bool CanGoBack
		{
			get
			{
				return this.frame.CanGoBack;
			}
		}

		/// <summary>
		/// Navigates to the most recent entry in back navigation history, if there is one.
		/// </summary>
		public void GoBack()
		{
			this.frame.GoBack();
		}

		/// <summary>
		/// Remove the most recent entry from the back stack.
		/// </summary>
		public void RemoveBackEntry()
		{
		}

		/// <summary>
		/// Navigates to a view associated with the specified <see cref="T:NavigatorViewModel">context</see>.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="target">The target view name.</param>
		/// <param name="extraData">The context view model.</param>
		public void Navigate(INavigable source, Type targetType, string target, object parameter)
		{
			var contentType = NavigableAttribute.GetContentType(target, targetType);
			this.frame.Navigate(contentType, parameter);
		}
	}
}
